var n_timer = 0;
var timers = {};

function startTimer(interval, countlimit, startcount, timerName, timerUUID)
{
    //console.log("11111startTimer", timerName, n_timer);
    stopTimer(timerName);
    if (interval >0) {
    } else {
        interval = 1000;
    }
    n_timer++;
    timers[n_timer] = {};
    timers[n_timer].timerId = n_timer;
    timers[n_timer].interval = interval;
    timers[n_timer].countlimit = countlimit;
    if (startcount > 0) {
        timers[n_timer].count = startcount;
    } else {
        timers[n_timer].count = 0;
    }
    timers[n_timer].timerName = timerName;
    timers[n_timer].timerUUID = timerUUID;
    var timer = timers[n_timer];
    //console.log("11111startTimer", timerName, n_timer, timer);
    timer.timer = setInterval(function(timer) {
        //console.log("11111timer", n_timer, timer, timers);
        //var timer1 = timers[timer.timerId];
        timer.count++;
        timer.time = new Date();
        //console.log("timer", JSON.stringify({"eventtype": "timer", "timer": timer}));
        postMessage({"eventtype": "timer", "timer": timer});
        if (timer.countlimit > 0 && timer.count>= timer.countlimit) {
            stopTimer(timer.timerId);
        }
    }, interval, timer);
    //console.log("timer", JSON.stringify({"eventtype": "timerStart", "timer": timer}));
    postMessage({"eventtype": "timerStart", "timer": timer});
}

function stopTimer(timerId, timerName, timerUUID)
{
    if (timerId == -9999) {
        for(let v in timers) {
            clearInterval(timers[v].timer);
            //console.log("timer", JSON.stringify({"eventtype": "timerEnd", "timer": timers[v]}));
            postMessage({"eventtype": "timerEnd", "timer": timers[v]});
        }
        timers = {};
        return;
    }

    var timer;
    if (timerId > 0) {
        timer = timers[timerId];
    } else if (timerUUID) {
        for(let v in timers) {
            if (timers[v].timerUUID == timerUUID) {
                timer = timers[v];
                break;
            }
        }
    } else if (timerName) {
        for(let v in timers) {
            if (timers[v].timerName == timerName) {
                timer = timers[v];
                break;
            }
        }
    }
    if (timer) {
        clearInterval(timer.timer);
        //console.log("timer", JSON.stringify({"eventtype": "timerEnd", "timer": timer}));
        delete timers[timer.timerId];
        postMessage({"eventtype": "timerEnd", "timer": timer});
    }
}

self.addEventListener("message", function(event) {
    switch(event.data.method)
    {
        case "startTimer":
            var params = event.data.params;
            if (params) {
                var ret = startTimer(params.interval, params.countlimit, params.startcount, params.timerName, params.timerUUID);
            }
            break;
        case "stopTimer":
            var params = event.data.params;
            if (params) {
                stopTimer(params.timerId, params.timerName, params.timerUUID);
            }
            break;
        case "stopAllTimers":
            stopTimer(-9999);
        default:
    }
}, false)